package com.sugon.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.sugon.consts.RepairCategory;
import com.sugon.dao.DevMaintenThirdpartyCbDao;
import com.sugon.entity.DevMaintenThirdpartyCbEntity;
import com.sugon.entity.DeviceInfoEntity;
import com.sugon.modules.device.consts.DevRepairTicketStage;
import com.sugon.modules.device.service.IDeviceRepairManager;
import com.sugon.modules.mold.service.IMoldRepairRecordManager;
import com.sugon.modules.sys.consts.SysDeptConst;
import com.sugon.service.DevMaintenThirdpartyCbService;
import com.sugon.service.DeviceInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 设备维修第三方回调数据Service实现类
 *
 * @author YuXD
 * @date 2022-07-30
 */
@Service("devMaintenThirdpartyCbService")
public class DevMaintenThirdpartyCbServiceImpl implements DevMaintenThirdpartyCbService {

    @Autowired
    private DevMaintenThirdpartyCbDao devMaintenThirdpartyCbDao;
    @Resource
    private IDeviceRepairManager deviceRepairManager;
    @Resource
    private IMoldRepairRecordManager moldRepairRecordManager;
    @Resource
    private DeviceInfoService deviceService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addThirdPartyCbData(DevMaintenThirdpartyCbEntity callbackData) {
        // 设置本次属于哪个维修阶段
        callbackData.setRepairStage(judgeRepairStage(callbackData));
        /*
            由于设备的同一个维修过程中可能会有多个记录，所以以第一个为准，其余的置为无效
         */
        List<DevMaintenThirdpartyCbEntity> callBackDatas = devMaintenThirdpartyCbDao.queryByDevIdAndStage(callbackData.getDevId(), callbackData.getCategory(), callbackData.getBusId(), callbackData.getRepairStage());
        if (CollectionUtil.isNotEmpty(callBackDatas)) {
            if (StrUtil.isNotEmpty(callbackData.getAction())) {
                callbackData.setIsValid(DevMaintenThirdpartyCbDao.VALID);
            } else {
                callbackData.setIsValid(DevMaintenThirdpartyCbDao.INVALID);
            }
        } else {
            callbackData.setIsValid(DevMaintenThirdpartyCbDao.VALID);
        }
        int save = save(callbackData);
        Assert.isTrue(save > 0);
        /*
            需要区分操作类型
            如果未填写，那么仍然按照正常逻辑走
            如果已填写，需要区分使用场景
         */
        // 如果回调数据有效，则需要创建工单或者更新维修工单或记录信息
        if (DevMaintenThirdpartyCbDao.VALID.equals(callbackData.getIsValid())) {
            // 需要区分是设备维修还是模具维修，然后走对应的逻辑
            if (RepairCategory.MOLD.name().equals(callbackData.getCategory())) {
                moldRepairRecordManager.handThirdPartyCallback(callbackData);
            } else {
                /*
                    应客户要求：针对铸造二车间设备的一键报修不会生成工单
                    即记录一键报修过来的数据，但不生成工单
                 */
                DeviceInfoEntity device = deviceService.queryObject(callbackData.getDevId());
                Assert.notNull(device);
                if (!StrUtil.startWith(device.getDeptCode(), SysDeptConst.ZZ2_CODE)) {
                    deviceRepairManager.handThirdPartyCallback(callbackData);
                }
            }
        }
        return true;
    }

    /**
     * 判定维修阶段
     *
     * @param callbackData 维修回调数据
     * @return
     */
    private Integer judgeRepairStage(DevMaintenThirdpartyCbEntity callbackData) {
        if (callbackData.getEndRepairTime() != null) {
            return DevRepairTicketStage.END_REPAIR;
        } else if (callbackData.getStartRepairTime() != null) {
            return DevRepairTicketStage.START_REPAIR;
        } else {
            // 统一默认为报修
            return DevRepairTicketStage.CALL_REPAIR;
        }
    }

    @Override
    public DevMaintenThirdpartyCbEntity queryObject(String id) {
        return devMaintenThirdpartyCbDao.queryObject(id);
    }

    @Override
    public List<DevMaintenThirdpartyCbEntity> queryByBusId(String busId) {
        return devMaintenThirdpartyCbDao.queryByBusId(busId);
    }

    @Override
    public List<DevMaintenThirdpartyCbEntity> queryList(Map<String, Object> map) {
        return devMaintenThirdpartyCbDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return devMaintenThirdpartyCbDao.queryTotal(map);
    }

    @Override
    public int save(DevMaintenThirdpartyCbEntity devMaintenThirdpartyCb) {
        devMaintenThirdpartyCb.setId(RandomUtil.randomString(32));
        devMaintenThirdpartyCb.setGmtCreate(DateUtil.date());
        return devMaintenThirdpartyCbDao.save(devMaintenThirdpartyCb);
    }

    @Override
    public int delete(String id) {
        return devMaintenThirdpartyCbDao.delete(id);
    }

    @Override
    public int deleteBatch(String[] ids) {
        return devMaintenThirdpartyCbDao.deleteBatch(ids);
    }
}
